let BaseController = require('./BaseController')
let fs = require('fs')
let path = require('path')
let cheerio = require('cheerio')
class 证券公司Controller extends BaseController {
    constructor() {
        super();
        this.pageIndex = 1;
        this.baseSrc = `d:/数据指标分类/${new Date().format('yyyyMMdd')}/待入库/09字典/券商/证券公司/`
        this.file = `证券公司数据_${new Date().format('yyyyMMdd')}.csv`
        this.encode = 'GBK'
        this.data = [];
    }
    /**
     * 获取url
     */
    getUrl(data) {
        return `http://data.eastmoney.com/stock/yybcx.html`
    }

    /**
     * 获取历史数据
     */
    async getHistory() {
        console.log('6证券公司Controller')
        let data = await this.getData(null, this.isCache)
        this.writeCsv(data)
    }

    /**
     * 从网页源码提取数据，并且格式化成arr
     */
    outputDataFormat(data) {
        let $ = cheerio.load(data, { decodeEntities: true });
        let arr = []
        $('.company-container a').map((i, item) => {
            let 证券公司ID = $(item).attr('href').match(/(\d+)\.html/)[1]
            let 证券公司名字 = $(item).text().trim()
            arr.push({ 证券公司ID, 证券公司名字 })
        })
        return arr
    }
    /**
     * 数据格式化
     */
    axiosDataFormat(data) {
        return JSON.parse(data.replace('var dGmnTeix=', '')).data
    }

    /**
     * 写csv
     */
    writeCsv(arr) {
        if (!fs.existsSync(this.baseSrc)) {
            this.mkdirs(this.baseSrc)
        }
        fs.writeFileSync(path.join(this.baseSrc, this.file), this.convertToCSV(arr, 'title'), 'UTF-8')
    }

    async main(startDate = '2015-01-01', isCache = true) {
        this.startDate = startDate;
        this.isCache = isCache;
        console.log('--->6证券公司Controller')
        await this.getHistory()
        this.writeCsv(this.data);
    }
}

let controller = new 证券公司Controller()
module.exports = controller
